<template>
  <div class="register-box">
    <p class="title">注册</p>
    <el-form :model="registerMsg" status-icon :rules="rules" ref="form" class="demo-ruleForm">
      <el-form-item  prop="username">
        <el-input v-model="registerMsg.username" placeholder="请输入用户名" ></el-input>
      </el-form-item>
      <el-form-item  prop="pass">
        <el-input type="password" v-model="registerMsg.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item  prop="checkPass">
        <el-input type="password" v-model="registerMsg.checkPass" placeholder="请再次输入密码"></el-input>
      </el-form-item>
      <el-form-item  prop="sex">
        <el-select   style="width:100%" class="register-input"  v-model="registerMsg.sex" placeholder="请选择性别">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
          <el-option label="保密" value="3"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item> -->
        <el-button type="primary" @click="registerClick" class="register-btn">注册</el-button>
      <!-- </el-form-item> -->
      <router-link to="/login" tag="span" class="go-login">已有账号，返回登录 >></router-link>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'register',
  data(){
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.registerMsg.checkPass !== '') {
            this.$refs.form.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerMsg.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return{
      registerMsg: {
          // name:'',
          // userName:'',
          username:'',
          password: '',
          checkPass: '',
          sex:''
      },
      rules:{
        username: [
          { required:true,message:'输入不能为空', trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        sex:[
          { required:true,message:'请选择性别', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    //注册
    registerClick() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$message({
            message: '注册成功',
            type: 'success'
          });
          // console.log(this.registerMsg,222)
        } else {
          this.$message({
            message: '好像有点问题哦',
            type: 'warning'
          });
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss">
.register-box{
  width:30%;
  position:absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  .title{
    font-size: 2rem;
    margin-bottom:.8rem;
  }
  // .register-form{
  //   .register-input{
  //     // margin-bottom:.8rem;
  //   }
    .register-btn{
      margin-right: 1rem;
      font-size: 1.2rem;
    }
    .go-login{
      color:#007bff;
      cursor: pointer;
    }
    & .go-login:hover{
      color:#034d9c;
      text-decoration: underline;
    }
  // }
}
</style>